//
//  RTStatusViewModel.swift
//  Weibo
//
//  Created by Brisinga on 16/3/7.
//  Copyright © 2016年 Root. All rights reserved.
//

import UIKit

class RTStatusViewModel: NSObject {
    
    var status: RTStatues?
    
    // 当前发这条微博的用户的认证图标
    var verifiedImage: UIImage?
    // 会员图标
    var membershipImage: UIImage?
    
    // 转发信息
    var retweetedText: String?
    
    
    // 转发数量
    var repostsCountStr: String?
    // 评论数量
    var commentsCountStr: String?
    // 点赞数量
    var attitudesCountStr: String?
    
    
    init(status: RTStatues) {
        
        super.init()
        
        self.status = status
        
        let verifiedType = status.user?.verified ?? -1
        
        
        // 判断认证等级
        switch verifiedType {
        
        case 1:
            
            verifiedImage = UIImage(named: "avatar_vip")
        
        case 2,3,5:
            
            verifiedImage = UIImage(named: "avatar_enterprise_vip")
            
        case 220:
            
            verifiedImage = UIImage(named: "avatar_grassroot")
            
        default:
            
            verifiedImage = nil
        
        }
        
        // 设置等级图标
        if let mbrank = status.user?.mbrank where mbrank > 0 {
        
            membershipImage = UIImage(named: "common_icon_membership_level\(mbrank)")
        }
        
        
        // 判断是否有转发内容
        if let screenName = status.retweeted_status?.user?.screen_name,text = status.retweeted_status?.text {
        
            retweetedText = "@\(screenName):\(text)"
            
        }else {
            
            print("都tm删了还看")
        }
        
        
        // // 计算转发评论赞的三个按钮显示的字符串
        // 先计算转发的内容
        
        repostsCountStr = stringWithCount(status.reposts_count,defaultString: "转发")
        
        commentsCountStr = stringWithCount(status.comments_count, defaultString: "评论")
        
        attitudesCountStr = stringWithCount(status.attitudes_count, defaultString: "赞")
    }
    
    
    // 获得toolbar对应数量
    private func stringWithCount(count: Int, defaultString: String)->String {
    
        /**
        以转发来说,如果有转发数据,那么就显示对应数量
        - 如果低于10000 --> 直接显示数字
        - 如果低于10900 --> 显示1万
        - 如果高于11000(12003) --> 显示 1.x 万
        */
        
        if count == 0 {
            
            return defaultString
            
        }else if count < 10000 {
            
            return "\(count)"
            
        }else {
        
            let result = CGFloat(count/10000)/10
            
            var resultStr = "\(result)万"
            
            if resultStr.containsString(".0") {
            
                resultStr = resultStr.stringByReplacingOccurrencesOfString(".0", withString: "")
                
            }
        return resultStr
        }
        
        
    }
}
